package com.baizhi.controller;

import com.baizhi.annotation.LogAnnotation;
import com.baizhi.annotation.LogType;
import com.baizhi.dao.CmfzUserDao;
import com.baizhi.entity.CmfzAdmin;
import com.baizhi.entity.CmfzUser;
import com.baizhi.service.CmfzUserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("user")
@Slf4j
public class CmfzUserController {
    @Autowired
    private CmfzUserService cus;
    @LogAnnotation(type = LogType.SELECT,value = "分页展示全部用户")
    @RequestMapping("selByNameOrPhone")
    public Map<String ,Object> selByNameOrPhone(@RequestParam(defaultValue = "1",value = "page") int pageNo,
                                                @RequestParam(defaultValue = "5",value = "limit") int pageSize
    ,@RequestParam(defaultValue = "null") String name,@RequestParam(defaultValue = "null") String phone){
        System.out.println(pageNo);
        System.out.println(name);
        System.out.println(phone+"----------------");
        Map<String,Object> map =new HashMap<>();
        Page<CmfzUser> cmfzUserPage;
        if((name.equals("null"))&&(phone.equals("null"))){
            cmfzUserPage = cus.allUser(pageNo, pageSize);

        }else {

            cmfzUserPage = cus.selectByNameOrPhone(name, phone, pageNo, pageSize);
        }

        //cmfzUserPage = cus.selectByNameOrPhone(name, phone, pageNo, pageSize);
        map.put("code",0);
        map.put("msg","");
        map.put("count",cmfzUserPage.getTotal());
        map.put("data",cmfzUserPage.getRecords());
        return map;
    }

    //展示全部

    @RequestMapping("allUser")

    public Map allUser(int page
            ,int limit){
        Page<CmfzUser> cmfzUserPage = cus.allUser(page, limit);

       Map<String,Object> map=new HashMap<>();
       map.put("code",0);
       map.put("msg","");
       map.put("data",cmfzUserPage.getRecords());
       map.put("count",cmfzUserPage.getTotal());
       return map;
    }

    //修改一个用户
    @RequestMapping("updUser")
    public Boolean updUser( CmfzUser user){
        try {
            cus.update(user);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return  false;
        }
    }

    //上传
    @RequestMapping("upload")
    public Map upload(MultipartFile file, HttpServletRequest request) throws IOException {
        //在文件名的前面添加上uuid避免文件名被覆盖
        String newName= UUID.randomUUID().toString()+ file.getOriginalFilename();

        //把文件复制到这个目录里面。
        file.transferTo(new File("D:/img/"+newName));

        //响应信息。code=0表示上传成功，src是图片上传后新的名字
        Map map=new HashMap();
        map.put("code",0);
        map.put("src",newName);
        return map;
    }

    //男女比例
    @Autowired
    @Resource
    private CmfzUserDao cud;
    @RequestMapping("mwb")

    public List<Map> mwb(){

        List<Map> maps = cud.selBySexCount();
        return maps;
    }

    //用户地区分布
    @RequestMapping("china")
    public List<Map> china(){

        List<Map> maps = cud.addressCount();
        return maps;
    }

    //三周内注册的数量变化
    @RequestMapping("threeWeeks")
    public List<Map> threeWeeks(){
        List<Map> maps = cud.threeWeeks();
        return maps;
    }

    //用户信息集合
    @RequestMapping("maps")
    public  Map maps() throws InterruptedException {
        Map stringMapMap = cus.tableMaps();
        return stringMapMap;
    }

}
